using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using SuperTicTacToe.Resources;
using Services.Menu;

namespace SuperTicTacToe.Menu
{
    class MainMenu : DrawableGameComponent
    {

        GraphicsDeviceManager GraphicsDeviceManager;
        InputManager input_manager;
        Texture2D buttonbg, screen_bg;
        SpriteFont font, font_t, font_p;
        MyButton credits, exit, newgame, title, higtScores;
        SpriteBatch spriteBatch;
        StateManager stateManager;
        protected float press_wait;
        protected readonly float PRESS_INTERVAL = 0.5f;
        public MainMenu(Game game, StateManager stateManager)
            : base(game)
        {
            this.stateManager = stateManager;
        }

        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            //stateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            //buttonbg = Game.Content.Load<Texture2D>("Levels/whiteTexture");
            buttonbg = Game.Content.Load<Texture2D>("Textures/emptyPixel");
            font = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            font_p = Game.Content.Load<SpriteFont>("Fonts/Arial26Ptx");
            font_t = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
            screen_bg = Game.Content.Load<Texture2D>("Textures/LevelBackground");
            var whiteBox = Game.Content.Load<Texture2D>("Textures/bar_box");
            Color textColor = new Color(8, 141, 217);
            
            newgame = new MyButton(new Rectangle(0, 300, 480, 80), Game.IsTrial() ? Strings.TrialVersion : Strings.Menu_NewGame, textColor, font);

            credits = new MyButton(new Rectangle(0, 400, 480, 80), Strings.Menu_Credits, textColor, font);
            higtScores = new MyButton(new Rectangle(0, 500, 480, 80), Strings.Menu_Scores, textColor, font);
            exit = new MyButton(new Rectangle(0, 600, 480, 80), Strings.Menu_Exit, textColor, font);


            title = new MyButton(new Rectangle(20, 0, 440, 200), Strings.Menu_Title, textColor, font_t);



            base.LoadContent();
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));
            Game.Components.Remove(input_manager);
            base.Dispose(disposing);
        }
        public void DisposeMenu()
        {
            Game.Services.RemoveService(typeof(IInputManager));
            Game.Components.Remove(input_manager);
            this.Dispose();
        }
        
        public override void Update(GameTime gameTime)
        {
            if (press_wait < PRESS_INTERVAL)
                press_wait += (float)gameTime.ElapsedGameTime.TotalSeconds;

            
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Game.Exit();
            }
            base.Update(gameTime);
        }

        Rectangle bg_rect = new Rectangle(0, 0, 480, 800);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();

            spriteBatch.Draw(screen_bg, bg_rect, Color.White);

            title.Draw(spriteBatch);
            
            newgame.Draw(spriteBatch);

            credits.Draw(spriteBatch);
            exit.Draw(spriteBatch);

            higtScores.Draw(spriteBatch);
            
            spriteBatch.End();
            base.Draw(gameTime);
        }
        Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            tp.X = (int)tap_position.X;
            tp.Y = (int)tap_position.Y;
            
                if (exit.tapped_on(tp))
                    Game.Exit();
                else if (newgame.tapped_on(tp))           
                    stateManager.SelectConfigMenu();
                else if (higtScores.tapped_on(tp))
                    stateManager.SelectHighScoresMenu();
                else if (credits.tapped_on(tp))
                    stateManager.SelectCreditsMenu();
                else if (title.tapped_on(tp))
                    stateManager.SelectCreditsMenu();
            }
        
    }
}



//using System;
//using System.Globalization;
//using System.Threading;
//using System.Collections.Generic;
//using System.Linq;
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Audio;
//using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Media;
//using System.Xml;
//using System.Reflection;
//using JigLibX.Physics;
//using JigLibX.Collision;
//using Services;
//using Utilities.Menu.UIElements;

//using Utilities.Menu;
//using System.Collections.ObjectModel;

//using Services.Menu;
//using Utilities;

//namespace SuperTicTacToe.Menu
//{
//    public class MainMenu : DrawableGameComponent
//    {
//        MyButton title, play, highScores, exit, credits;
//        StateManager stateManager;
//        InputManager input_manager;
//        SpriteBatch spriteBatch;
//        SpriteFont arialHuge, titles, names, arial18, arial14, arial16;
//        Texture2D emptyTexture, whiteTexture;
//        public MainMenu(Game game, StateManager stateManager)
//            : base(game)
//        {
//            this.stateManager = stateManager;
//        }
//        public override void Initialize()
//        {
//            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
//            //StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
//            Game.Services.RemoveService(typeof(IInputManager));
//            input_manager = new InputManager(Game);
//            Game.Components.Add(input_manager);
//            input_manager.OnTap += on_tap;
//            base.Initialize();
//        }
//        protected override void LoadContent()
//        {
//            arialHuge = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");

//            titles = Game.Content.Load<SpriteFont>("Fonts/Arial54Ptx");
//            arial16 = Game.Content.Load<SpriteFont>("Fonts/Arial16Ptx");
//            names = Game.Content.Load<SpriteFont>("Fonts/Arial20Ptx");
//            arial18 = Game.Content.Load<SpriteFont>("Fonts/Arial18Ptx");
//            arial14 = Game.Content.Load<SpriteFont>("Fonts/Arial14Ptx");
//            emptyTexture = Game.Content.Load<Texture2D>("Textures/emptyPixel");
//            whiteTexture = Game.Content.Load<Texture2D>("Textures/whitePixel");
//            var color = new Color(8, 141, 217);
            


//            base.LoadContent();
//        }
//        public override void Update(GameTime gameTime)
//        {
//            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
//            {
//                Game.Exit();
//            } 
//            base.Update(gameTime);
//        }
//        public override void Draw(GameTime gameTime)
//        {
//            GraphicsDevice.Clear(Color.White);
//            base.Draw(gameTime);
//        }
//        Point tp = new Point();
//        void on_tap(Vector2 tap_position)
//        {
//            tp.X = (int)tap_position.X;
//            tp.Y = (int)tap_position.Y;

//            //if (menu.tapped_on(tp))
//            //    StateManager.SelectMainMenu();
//        }
//    }
//}
